var ProductModule = function () {
    var editable = function () {

        return {
            init: function () {
                var oTable = $('#modelEditable').dataTable({
                	//"autoWidth":false,
                	"scrollX": true,
                	"lengthMenu": [
                                   [10, 15, 20, 50],
                                   [10, 15, 20, 50]
                               ],
                    "displayLength": 10,
                    "language": {
                        "lengthMenu": "每页 _MENU_ 条记录",
                        "paginate": {
                            "previous": "上一页",
                            "next": "下一页"
                        },
                        "info" : "从 _START_ 到  _END_ 条，总记录为 _TOTAL_ 条",    
                        "infoEmpty" : "记录数为0",
                     },
                     "columnDefs": [
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [0]
            						},
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [1]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [2]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [3]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [4]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [5]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [6]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [7]
            						},
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [8]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [9]
            						}
                                ],
                     "columns": [{ "data": "productCode" },
                                { "data": "briefName" },
                                { "data": "duration" },
                                { "data": "prospectiveEarnings"},
                                { "data": "incomeType" },
                                { "data": "scale" },
                                { "data": "state" },
                                { "data": "orders" },
                                { "data": "auditState" }],
                   "filter" : false,
                   "processing": false,
                   "serverSide": true,
                   "ajaxSource": ctx + "/product/queryAudit",
                   "serverMethod": "post",
                   "fnRowCallback" : function(nRow, aData, displayIndex) {
                	   nRow.children[0].id = aData.id;
                	   
//                	   var date = aData.createTime.substring(0,10);
//                	   $('td:eq(3)', nRow).html(date);
//                	   date = aData.endTime.substring(0,10);
//                	   $('td:eq(4)', nRow).html(date);
//                	   var contact = "<center>" + aData.contact + "<br>" + aData.mobile + "</center>";
//                	   $('td:eq(6)', nRow).html(contact);
                	   $('td:eq(2)', nRow).html(aData.duration+"&nbsp;月");
                	   $('td:eq(5)', nRow).html(formatCurrency(aData.scale)+"&nbsp;元");
                	   var incomeType;
                	   if(aData.incomeType == 0){
                		   incomeType = "固定";
                	   }else if(aData.incomeType == 1){
                		   incomeType = "浮动";
                	   }else if(aData.incomeType == 2){
                		   incomeType = "固定+浮动";
                	   }
                	   $('td:eq(4)', nRow).html(incomeType);
                	   
                	   var state;
                	   if(aData.state == 0){
                		   state = "即将上线";
                	   }else if(aData.state == 1){
                		   state = "正在募集中";
                	   }else if(aData.state == 2){
                		   state = "募集完成";
                	   }
                	   $('td:eq(6)', nRow).html(state);
                	   
                	   $('td:eq(8)', nRow).html(aData.auditState==1?"已审核":"审核未通过 ");
                	   
                	   //var opera = '<a class="productAudit fa fa-check-square-o fa-lg" title="产品审核" href="javascript:;"></a>';
                	   var opera;
                	    if(aData.auditState == 1){
                		   opera =  '<a class="detail fa fa-bars fa-lg" title="详情" href="javascript:;"></a>&nbsp;&nbsp;'+
                		            '<a class="auditNoPass fa fa-times-circle-o fa-lg" title="审核不通过" href="javascript:;"></a>';
                	   }else{
                		   opera = '<a class="detail fa fa-bars fa-lg" title="详情" href="javascript:;"></a>&nbsp;&nbsp;'+
                		   		   '<a class="auditPass fa fa-check-circle-o fa-lg" title="审核通过" href="javascript:;"></a>';
                	   }
                	   
                	   $('td:eq(9)', nRow).html(opera);
                       return nRow;
                   },
                   "fnServerData" : function(ajaxSource, dataSet, fnCallback) {
	                   var json = "";
	                   var s;
	                   for (var i = 0; i < dataSet.length; i++) {
	                       s = dataSet[i];
	                   	   if (s.name == 'sEcho') {
	                   		   json = json + '"echo":' + s.value;
		                   } else if (s.name == 'iDisplayStart') {
		                	   json = json + ',"start":' + s.value;
		                   } else if (s.name == 'iDisplayLength') {
		                	   json = json + ',"pageSize":' + s.value;
		                   }
	                   }
	                   var form = $("form#queryForm").serializeJSON();
                   	   var js = form.replace('}', ',');
                   	   json = js + json + ',"orderBy":"create_time desc"' +"}";
                       $.ajax({
                       	"contentType":"application/json; charset=UTF-8",
                           "dataType" : 'json',
                           "type" : "POST",
                           "url" : ajaxSource,
                           "data" : json,
                           "success" : fnCallback
                       });
                   },
                });
               
                $('#queryBtn').click(function(){
                	oTable.fnClearTable();
                });
                
                $('#addBtn').click(function(){
                	window.location.href = "product/add";
                });
                
                $('#modelEditable').on("click", "a.detail", (function (e) {
                    e.preventDefault();

                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                	
                    window.location.href = "product/detail/"+id;
                }));
                
                //产品审核
                $('#modelEditable').on("click", "a.auditPass", (function (e) {
                    e.preventDefault();
                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                    
                    
                    bootbox.confirm(
                    		"确定审核通过?",
                    		function(result) {
                    			if (result) {
                    				$.ajax({
                    					type: "post",
                    					url: ctx + "/product/auditPass/" + id,
                    					dataType: "json",
                    					success: function(msg) {
                    						if (msg.code == 1) {
                    							bootbox.alert("修改信息成功！");
                    							oTable.fnDeleteRow(nRow);
                    							window.location.href = "product/listAudit";
                    						} else {
                    							bootbox.alert("修改信息失败！");
                    						}
                    					},
                    					error: function(msg) {
                    						bootbox.alert(msg.message);
                    					}
                    				});
                    			}
                    		});
                    
                }));
                $('#modelEditable').on("click", "a.auditNoPass", (function (e) {
                    e.preventDefault();
                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                    
                    
                    bootbox.confirm(
                    		"确定审核不通过?",
                    		function(result) {
                    			if (result) {
                    				$.ajax({
                    					type: "post",
                    					url: ctx + "/product/auditNoPass/" + id,
                    					dataType: "json",
                    					success: function(msg) {
                    						if (msg.code == 1) {
                    							bootbox.alert("修改信息成功！");
                    							oTable.fnDeleteRow(nRow);
                    							window.location.href = "product/listAudit";
                    						} else {
                    							bootbox.alert("修改信息失败！");
                    						}
                    					},
                    					error: function(msg) {
                    						bootbox.alert(msg.message);
                    					}
                    				});
                    			}
                    		});
                   
                }));
            }

        };

    }();
    
    return {
        init: function () {
        	editable.init();
        }
    };
}();